	//OEM设置
	function showOEM(type, id) {
		var d = {};
		if(type == 1) {
			d.userId = token_id;
		} else {
			d.userId = id;
		}
		$.ajax({
			url: url + "/sys/agOemSet/findOemSet/" + d.userId,
			type: 'POST',
			data: JSON.stringify(d),
			dataType: 'json',
			contentType: "application/json",
			beforeSend: function(XMLHttpRequest) {
				XMLHttpRequest.setRequestHeader("X-Token", token); 
			},
			useDefaultXhrHeader: false,
			success: function(result) {
				if(result.code == '0') {
					var data = result.data;
					if(data == null) {
						$("#dspOemModal").modal();
						return false;
					}
					$(".agAdvertLogo").attr("src", data.agAdvertLogo);
					$(".agIndexLogo").attr("src", data.agIndexLogo);
					$(".agLogo").attr("src", data.agLogo);
					$(".agNavigationLogo").attr("src", data.agNavigationLogo);
					$(".agWebLogo").attr("src", data.agWebLogo);
					$(".agLoginBg").attr("src", data.agLoginBg);
					$(".agCopyrightMsg").val(data.agCopyrightMsg);
					$(".agIndexLogoUrl").val(data.agIndexLogoUrl);
					$(".agWebTitle").val(data.agWebTitle);
					$(".agWrodTitle").val(data.agWrodTitle);

					$(".agid").attr("data", data.id);

					//				启用自有域名
					if(data.agOemUrls == null || data.agOemUrls == "") {
						console.log("1");
						var html = '<div class="form-group ng-scope" ng-repeat="item in oemList track by $index"><label>启用自有域名：</label>';
						html += '<input type="text" style="display: inline-block;vertical-align: middle;" oemdata="1" placeholder="ads.hubcloud.com.cn" class="form-control ng-pristine ng-untouched ng-valid ng-empty oem_input"><a target="_blank" href="//">测试</a>';
						html += '<i  class="icon-add ng-scope addUrl_i"></i></div>';
						$(".oem_list").html(html);
					} else {
						console.log("2");
						var html = '';
						var data1 = data.agOemUrls
						for(var i in data1) {
							if(i != data1.length - 1) {
								html += '<div class="form-group ng-scope" ng-repeat="item in oemList track by $index"><label>启用自有域名：</label>';
								html += '<input type="text" style="display: inline-block;vertical-align: middle;" value="' + data1[i].oemUrl + '" data="' + data1[i].id + '" data1="' + data1[i].oemId + '" oemdata="2" placeholder="ads.hubcloud.com.cn" class="form-control ng-valid ng-not-empty ng-dirty ng-touched oem_input">';
								html += '<a target="_blank" href="//' + data1[i].oemUrl + '">测试</a><i class="icon-close ng-scope removeUrl_i"></i></div>';
							} else {
								html += '<div class="form-group ng-scope" ng-repeat="item in oemList track by $index"><label>启用自有域名：</label>';
								html += '<input type="text" style="display: inline-block;vertical-align: middle;" value="' + data1[i].oemUrl + '" data="' + data1[i].id + '" data1="' + data1[i].oemId + '" oemdata="2" placeholder="ads.hubcloud.com.cn" class="form-control ng-pristine ng-untouched ng-valid ng-empty oem_input">';
								html += '<a target="_blank" href="//' + data1[i].oemUrl + '">测试</a><i class="icon-add ng-scope addUrl_i"></i></div>';
							}
						}
						$(".oem_list").html(html);
					}
					$("#dspOemModal").modal();

				} else {
					isErr(result);
				}
			},
			err: function(err) {
				layer.msg("请求失败");
			}
		});
	}

	$(document).on("keyup", ".oem_input", function() {
		$(this).next().attr("href", "//" + $(this).val());
	})
	//	删除域名
	$(document).on("click", ".removeUrl_i", function() {
		$(this).parent().remove();
	})
	//	添加域名
	$(document).on("click", ".addUrl_i", function() {
		$(this).addClass("icon-close").removeClass("icon-add");
		$(this).addClass("removeUrl_i").removeClass("addUrl_i");
		var html1 = '<div class="form-group ng-scope" ng-repeat="item in oemList track by $index"><label>启用自有域名：</label>';
		html1 += '<input type="text" style="display: inline-block;vertical-align: middle;" value="" oemdata="1" placeholder="ads.hubcloud.com.cn" class="form-control ng-pristine ng-untouched ng-valid ng-empty oem_input">';
		html1 += '<a target="_blank" href="//">测试</a><i class="icon-add ng-scope addUrl_i"></i></div>';
		$(".oem_list").append(html1);
	})

	//修改OEM
	function saveOEM() {
		var agAdvertLogo = $(".agAdvertLogo").attr("src");
		var agIndexLogo = $(".agIndexLogo").attr("src");
		var agLogo = $(".agLogo").attr("src");
		var agNavigationLogo = $(".agNavigationLogo").attr("src");
		var agWebLogo = $(".agWebLogo").attr("src");
		var agLoginBg = $(".agLoginBg").attr("src");
		var agCopyrightMsg = $(".agCopyrightMsg").val();
		var agIndexLogoUrl = $(".agIndexLogoUrl").val();
		var agWebTitle = $(".agWebTitle").val();
		var agWrodTitle = $(".agWrodTitle").val();

		var d = {};
		d.agAdvertLogo = agAdvertLogo;
		d.agIndexLogo = agIndexLogo;
		d.agLogo = agLogo;
		d.agNavigationLogo = agNavigationLogo;
		d.agWebLogo = agWebLogo;
		d.agLoginBg = agLoginBg;
		d.agCopyrightMsg = agCopyrightMsg;
		d.agIndexLogoUrl = agIndexLogoUrl;
		d.agWebTitle = agWebTitle;
		d.agWrodTitle = agWrodTitle;
		d.id = $(".agid").attr("data");
		var kk = [];
		for(var i = 0; i < $(".oem_input").length; i++) {
			if($(".oem_input").eq(i).attr("omedata") == "2") {
				if($(".oem_input").eq(i).val() == "") {
					layer.msg("请输入域名");
					return false;
				} else {
					kk[i] = { "id": $(this).attr("data"), "oemId": $(this).attr("data1"), "oemUrl": $(".oem_input").eq(i).val() };
				}
			} else {
				if($(".oem_input").eq(i).val() != "") {
					kk[i] = { "oemUrl": $(".oem_input").eq(i).val() };
				}
			}
		}
		d.agOemUrls = kk;
		$.ajax({
			url: url + "/sys/agOemSet/editOemSetMsg",
			type: 'POST',
			//		参数需转为字符串
			data: JSON.stringify(d),
			//		json跨域
			dataType: 'json',
			contentType: "application/json",
			beforeSend: function(XMLHttpRequest) {
				XMLHttpRequest.setRequestHeader("X-Token", token); 
			},
			useDefaultXhrHeader: false,
			success: function(result) {
				if(result.code == '0') {
					layer.msg(result.msg, { time: 2 * 1000 }, function() {
						$("#dspOemModal").modal('hide');
						$('.modal-backdrop').remove();
					});
				} else {
					isErr(result);
				}
			},
			err: function(err) {
				layer.msg("请求失败");
			}
		});
	}
	//	上传图片
	$(".imgg").on("click", function() {
		$("#file" + $(this).attr("data")).attr("data", $(this).attr("data"));
		$("#file" + $(this).attr("data")).click();
	})
	$("input[type='file']").on("change", function() {
		doUpload($(this).attr("data"));
	})
	//上传图片接口
	function doUpload(a) {
		var formData = new FormData($("#form" + a)[0]);
		$.ajax({
			url: url + "/common/upload",
			type: 'POST',
			//		参数需转为字符串
			data: formData,
			//		json跨域
			dataType: 'json',
			//		contentType类型为json
			contentType: false,
			processData: false,

			//		请求头加上X-Token 
			 beforeSend: function(XMLHttpRequest) {
				//		token 为页面所获取
				   
				XMLHttpRequest.setRequestHeader("X-Token", token); 
			},
			useDefaultXhrHeader: false,
			success: function(result) {
				if(result.code == '0') {
					$("#file" + a).parent().parent().find(".form-img").attr("src", result.data[0].url);
					$("#file" + a).parent().parent().find(".form-img").attr("data_d", "2");
				} else {
					isErr(result);
				}
			},
			err: function(err) {
				layer.msg("请求失败");
			}
		});
	}

	//按钮切换
	$(".agType").on("click", function() {
		$(".agType").removeClass("checked");
		$(this).addClass("checked");
	})
	$(".agStrategy").on("click", function() {
		$(".agStrategy").removeClass("checked");
		$(this).addClass("checked");
	})
	$(".agentModal11_agStrategy").on("click", function() {
		$(".agentModal11_agStrategy").removeClass("checked");
		$(this).addClass("checked");
	})

	//添加代理商
	var ppll = true;
	$(".addAgent_btn").on("click", function() {
		if(ppll) {
			ppll = false;
			var username = $(".username").val();
			var password = $(".password").val();
			var cppassword = $(".cppassword").val();
			var agCompayName = $(".agCompayName_1").val();
			var agPerson = $(".agPerson").val();
			var agPhone = $(".agPhone").val();
			var agProfit = $(".agProfit").val();
			var agType = $("#agentModal .checked").eq(0).attr("value");
			var agStrategy = $("#agentModal .checked").eq(1).attr("value");
			if(!username) {
				layer.msg("请输入账号");
				ppll = true;
				return false;
			}
			if(!password) {
				layer.msg("请输入密码");
				ppll = true;
				return false;
			}
			if(password != cppassword) {
				layer.msg("两次密码输入不一致");
				ppll = true;
				return false;
			}
			if(!agCompayName) {
				layer.msg("请输入公司名称");
				ppll = true;
				return false;
			}
			if(!agPerson) {
				layer.msg("请输入联系人");
				ppll = true;
				return false;
			}
			if(!agPhone) {
				layer.msg("请输入手机号");
				ppll = true;
				return false;
			}
			if(!agProfit) {
				layer.msg("请输入利润设置");
				ppll = true;
				return false;
			}
			var d = {};
			d.username = username;
			d.password = password;
			d.agCompayName = agCompayName;
			d.agPerson = agPerson;
			d.agPhone = agPhone;
			d.agProfit = agProfit;
			d.agStrategy = agStrategy;
			d.agType = agType;
			$.ajax({
				url: url + "/sys/addAgentUser",
				type: 'POST',
				//		参数需转为字符串
				data: JSON.stringify(d),
				//		json跨域
				dataType: 'json',
				contentType: "application/json",
				beforeSend: function(XMLHttpRequest) {
					XMLHttpRequest.setRequestHeader("X-Token", token); 
				},
				useDefaultXhrHeader: false,
				success: function(result) {
					if(result.code == '0') {
						layer.msg(result.msg, { time: 2 * 1000 }, function() {
							p = 1;
							tiaozhuan(p);
							$("#agentModal").modal('hide');
							$('.modal-backdrop').remove();
						});
					} else {
						isErr(result);
					}
				},
				err: function(err) {
					layer.msg("请求失败");
				},
				complete: function() {
					ppll = true;
				}
			});
		}
	})

	//代理商列表
	var p = 1;

	function tiaozhuan(p) {
		var agCompayName = $(".agCompayName").val();
		var pageSize = $('.ui-select-pagesize').val();
		var d = {};
		d.page = p;
		d.agCompayName = agCompayName;
		d.pageSize = pageSize;
		$.ajax({
			url: url + "/sys/addAgUserList?" + agCompayName + "," + p + "," + pageSize,
			type: 'POST',
			//		参数需转为字符串
			data: JSON.stringify(d),
			//		json跨域
			dataType: 'json',
			contentType: "application/json",
			//		请求头加上X-Token 
			 beforeSend: function(XMLHttpRequest) {
				//		token 为页面所获取
				   
				XMLHttpRequest.setRequestHeader("X-Token", token); 
			},
			useDefaultXhrHeader: false,
			success: function(result) {
				if(true) {
					var data = result.data.rows;
					var html = '';
					for(var i in data) {
						html += '<tr>';
						if(data[i].userStatus == "0") {
							html += '<td><div class="switch ng-isolate-scope userStatus" data1="' + data[i].id + '" data2="0"></div>';
						} else {
							html += '<td><div class="switch ng-isolate-scope open userStatus" data1="' + data[i].id + '" data2="1"></div>';
						}
						html += '</td><td class="ng-binding">' + data[i].id + '</td>';
						html += '<td><a href="javascript:void(0)" target="_blank" onclick="goAccount(' + data[i].id + ')">' + data[i].username + '</a>';
						//					公司类型判断
						if(data[i].agType == "0") {
							html += '</td><td><p>普通广告公司</p></td>';
						} else {
							html += '</td><td><p>直客广告公司</p></td>';
						}

						//					溢价类型
						//					html += '<td><p class="ng-scope"></p></td>';
						//					溢价策略判断
						if(data[i].agStrategy == "0") {
							html += '<td><p class="ng-scope">出价回推<button class="dsp-btn btn-blue" onclick="changeMode(0,' + data[i].id + ')">修改策略</button></p></td>';
						} else {
							html += '<td><p class="ng-scope">固定出价<button class="dsp-btn btn-blue" onclick="changeMode(1,' + data[i].id + ')">修改策略</button></p></td>';
						}
						html += '<td class="ng-binding">' + data[i].agCompayName + '</td>';
						html += '<td class="ng-binding">' + data[i].agPerson + '</td><td class="ng-binding">' + data[i].agPhone + '</td>';
						html += '<td class="ng-binding">' + data[i].amount + '</td><td class="ng-scope">';
						html += '<a href="javascript:;" onclick="showProfits(' + data[i].agProfit + ',' + data[i].id + ');" class="ng-binding">' + data[i].agProfit + '%</a></td><td>';
						html += '<a href="javascript:;" onclick="showRePwd(' + data[i].id + ')" class="ng-scope">重置密码</a>';
						html += '<a href="javascript:;" data1="' + data[i].agCompayName + '" data2="' + data[i].agPerson + '" data3="' + data[i].agPhone + '" data4="' + data[i].agProfit + '" data5="' + data[i].agStrategy + '" onclick="showAgent(' + data[i].id + ',this)" class="ng-scope">编辑</a>';
						html += '<a href="javascript:;" onclick="showOEM(2,' + data[i].id + ')">OEM设置</a></td></tr>';
					}
					$(".list_body").html(html);
					var r = result.data.total;
					paging(p, pageSize, r);
					//			    修改账号状态
					$(".userStatus").on("click", function() {
						var userStatus = $(this).attr("data2") == "0" ? "1" : "0";
						var d = {};
						d.userStatus = userStatus;
						d.userId = $(this).attr("data1");
						$.ajax({
							url: url + "/sys/updateStatus",
							type: 'POST',
							//		参数需转为字符串
							data: JSON.stringify(d),
							//		json跨域
							dataType: 'json',
							contentType: "application/json",
							beforeSend: function(XMLHttpRequest) {
								XMLHttpRequest.setRequestHeader("X-Token", token); 
							},
							useDefaultXhrHeader: false,
							success: function(result) {
								if(result.code == '0') {
									layer.msg(result.msg, { time: 2 * 1000 }, function() {
										p = 1;
										tiaozhuan(p);
									});
								} else {
									isErr(result);
								}
							},
							err: function(err) {
								layer.msg("请求失败");
							}
						});
					})
				} else {
					isErr(result);
				}
			},
			err: function(err) {
				layer.msg("请求失败");
			}
		});
	}

	tiaozhuan(1);

	// 显示修改广告公司弹框
	function showAgent(id, obj) {
		$(".agentModal11_btn").attr("data", id);
		$(".agentModal11_agCompayName").val($(obj).attr("data1"));
		$(".agentModal11_agPerson").val($(obj).attr("data2"));
		$(".agentModal11_agPhone").val($(obj).attr("data3"));
		$(".agentModal11_agProfit").val($(obj).attr("data4"));
		if($(obj).attr("data5") == "0") {
			$(".agentModal11_agStrategy").eq(0).addClass("checked");
			$(".agentModal11_agStrategy").eq(1).removeClass("checked");
		} else {
			$(".agentModal11_agStrategy").eq(0).removeClass("checked");
			$(".agentModal11_agStrategy").eq(1).addClass("checked");
		}
		$("#agentModal11").modal()
	}

	//编辑代理商
	var ppll1 = true;
	$(".agentModal11_btn").on("click", function() {
		if(ppll1) {
			ppll1 = false;
			var agCompayName = $(".agentModal11_agCompayName").val();
			var agPerson = $(".agentModal11_agPerson").val();
			var agPhone = $(".agentModal11_agPhone").val();
			var agProfit = $(".agentModal11_agProfit").val();
			var agStrategy = $("#agentModal11 .checked").eq(0).attr("value");
			if(!agCompayName) {
				layer.msg("请输入公司名称");
				ppll1 = true;
				return false;
			}
			if(!agPerson) {
				layer.msg("请输入联系人");
				ppll1 = true;
				return false;
			}
			if(!agPhone) {
				layer.msg("请输入手机号");
				ppll1 = true;
				return false;
			}
			if(!agProfit) {
				layer.msg("请输入利润设置");
				ppll1 = true;
				return false;
			}
			var d = {};
			d.agCompayName = agCompayName;
			d.agPerson = agPerson;
			d.agPhone = agPhone;
			d.agProfit = agProfit;
			d.agStrategy = agStrategy;
			d.sysUserId = $(this).attr("data");
			$.ajax({
				url: url + "/sys/editMsg",
				type: 'POST',
				//		参数需转为字符串
				data: JSON.stringify(d),
				//		json跨域
				dataType: 'json',
				contentType: "application/json",
				beforeSend: function(XMLHttpRequest) {
					XMLHttpRequest.setRequestHeader("X-Token", token); 
				},
				useDefaultXhrHeader: false,
				success: function(result) {
					if(result.code == '0') {
						layer.msg(result.msg, { time: 2 * 1000 }, function() {
							p = 1;
							tiaozhuan(p);
							$("#agentModal11").modal('hide');
							$('.modal-backdrop').remove();
						});
					} else {
						isErr(result);
					}
				},
				err: function(err) {
					layer.msg("请求失败");
				},
				complete: function() {
					ppll1 = true;
				}
			});
		}
	})

	//重置密码弹框
	function showRePwd(id) {
		$(".newPassword_1").val("");
		$(".newPassword_cp").val("");
		$(".rePwd_btn").attr("data", id);
		layer.open({
			type: 1,
			title: "重置密码",
			content: $('.rePwd'),
			area: ["400px", "280px"],
			end: function() { //无论是确认还是取消，只要层被销毁了，end都会执行，不携带任何参数。layer.open关闭事件
				$(".rePwd").css("display", "none");
			}
		})
	}

	//重置密码
	var ppll_pass = true;
	$(".rePwd_btn").on("click", function() {
		if(ppll_pass) {
			ppll_pass = false;
			var newPassword = $(".newPassword_1").val();
			var newPassword_cp = $(".newPassword_cp").val();
			if(!newPassword) {
				layer.msg("请输入密码");
				ppll_pass = true;
				return false;
			}
			if(newPassword != newPassword_cp) {
				layer.msg("两次密码输入不一致");
				ppll_pass = true;
				return false;
			}
			var d = {};
			d.newPassword = newPassword;
			d.id = Number($(this).attr("data"));
			$.ajax({
				url: url + "/sys/resetPassword",
				type: 'POST',
				//		参数需转为字符串
				data: JSON.stringify(d),
				//		json跨域
				dataType: 'json',
				contentType: "application/json",
				beforeSend: function(XMLHttpRequest) {
					XMLHttpRequest.setRequestHeader("X-Token", token); 
				},
				useDefaultXhrHeader: false,
				success: function(result) {
					if(result.code == '0') {
						layer.msg(result.msg, { time: 2 * 1000 }, function() {
							layer.closeAll();
						});
					} else {
						isErr(result);
					}
				},
				err: function(err) {
					layer.msg("请求失败");
				},
				complete: function() {
					ppll_pass = true;
				}
			});
		}
	})

	//利润设置弹框
	function showProfits(a, c) {
		$(".profits_agProfit").val(a);
		$(".profits_btn").attr("data", c);
		layer.open({
			type: 1,
			title: "利润设置",
			content: $('.profits'),
			area: ["295px", "250px"],
			end: function() { //无论是确认还是取消，只要层被销毁了，end都会执行，不携带任何参数。layer.open关闭事件
				$(".profits").css("display", "none");
			}
		})
	}

	//利润设置按钮
	var ppll_profits = true;
	$(".profits_btn").on("click", function() {
		if(ppll_profits) {
			ppll_profits = false;
			var agProfit = $(".profits_agProfit").val();
			if(!agProfit) {
				layer.msg("请输入利润");
				ppll_profits = true;
				return false;
			}
			var d = {};
			d.agProfit = agProfit;
			d.userId = $(this).attr("data");
			$.ajax({
				url: url + "/sys/editSet",
				type: 'POST',
				//		参数需转为字符串
				data: JSON.stringify(d),
				//		json跨域
				dataType: 'json',
				contentType: "application/json",
				beforeSend: function(XMLHttpRequest) {
					XMLHttpRequest.setRequestHeader("X-Token", token); 
				},
				useDefaultXhrHeader: false,
				success: function(result) {
					if(result.code == '0') {
						layer.msg(result.msg, { time: 2 * 1000 }, function() {
							p = 1;
							tiaozhuan(p);
							layer.closeAll();
						});
					} else {
						isErr(result);
					}
				},
				err: function(err) {
					layer.msg("请求失败");
				},
				complete: function() {
					ppll_profits = true;
				}
			});
		}
	})

	//修改策略弹框
	function changeMode(d, c) {
		var str = d == '0' ? '目前使用的溢价策略是“出价回推”模式；<br/>是否确认切换为“固定出价”模式？' : '目前使用的溢价策略是“固定出价”模式；<br/>是否确认切换为“出价回推”模式？';
		var kk = d == "0" ? "1" : "0";
		layer.confirm(str, {
			btn: ['确定', '取消']
		}, function() {
			var d = {
				userId: c,
				agStrategy: kk
			}
			$.ajax({
				url: url + "/sys/editStrategy",
				type: 'POST',
				//		参数需转为字符串
				data: JSON.stringify(d),
				//		json跨域
				dataType: 'json',
				contentType: "application/json",
				beforeSend: function(XMLHttpRequest) {
					XMLHttpRequest.setRequestHeader("X-Token", token); 
				},
				useDefaultXhrHeader: false,
				success: function(result) {
					if(result.code == '0') {
						layer.msg(result.msg, { time: 2 * 1000 }, function() {
							p = 1;
							tiaozhuan(p);
						});
					} else {
						isErr(result);
					}
				},
				err: function(err) {
					layer.msg("请求失败");
				}
			});
		})
	}

	//打开广告主页面
	function goAccount(id) {
		var datass = {};
		datass.userId = id;
		$.ajax({
			url: url + "/sys/agOemSet/findOemSet/" + datass.userId,
			type: 'POST',
			data: JSON.stringify(datass),
			dataType: 'json',
			contentType: "application/json",
			beforeSend: function(XMLHttpRequest) {
				XMLHttpRequest.setRequestHeader("X-Token", token); 
			},
			useDefaultXhrHeader: false,
			success: function(result1) {
				if(result1.code == '0') {
					var data = result1.data;
					var oemurls = "login.html";
					if(data.agOemUrls.length == 0){
						
					}else{
						oemurls = data.agOemUrls[0].oemUrl;
					}
					

					
					var param4 = "/agency/manage/namepwd?aduserid=" + id;
					$.ajax({
						url: url + param4,
						type: "post",
						dataType: 'json',

						contentType: "application/json",
						useDefaultXhrHeader: false,
						 beforeSend: function(XMLHttpRequest) {
							XMLHttpRequest.setRequestHeader("X-Token", token); 
						},
						success: function(result) {
							if(result) {
								window.open(oemurls+"?username=" + result[0].userName + "&password=" + result[0].password);
								//				console.log(result[0].userName);
								//				console.log(result[0].password);
							} else {
								isErr(result);
							}
						},
						err: function(err) {
							layer.msg("请求失败");
						}
					});
				}
			},
			err: function(err) {
				layer.msg("请求失败");
			}
		});

	}

	$(".seek_btn").on("click", function() {
		p = 1;
		tiaozhuan(p);
	})